# app/main.py
from fastapi import FastAPI
from app.config import settings
from app.routers import asr, health, aliyun_voice_clone, eage_tts
from app.models.asr_model import asr_model_manager
from app.utils.logger import setup_logger

logger = setup_logger("main")

def create_app() -> FastAPI:
    """
    创建并配置FastAPI应用
    """
    # 创建FastAPI应用
    app = FastAPI(
        title=settings.APP_NAME,
        description=settings.APP_DESCRIPTION,
    )
    
    # 注册路由
    app.include_router(asr.router, prefix=settings.API_PREFIX)
    app.include_router(aliyun_voice_clone.router, prefix=settings.API_PREFIX)
    app.include_router(eage_tts.router, prefix=settings.API_PREFIX)
    app.include_router(health.router)
    
    
    # 启动事件
    @app.on_event("startup")
    async def startup_event():
        logger.info("正在启动应用...")
        await asr_model_manager.load_model()
        logger.info("应用启动完成")
    
    # 关闭事件
    @app.on_event("shutdown")
    async def shutdown_event():
        logger.info("应用正在关闭...")
    
    return app

# 创建应用实例
app = create_app()

# 直接运行时的入口点
if __name__ == "__main__":
    import uvicorn
    logger.info(f"启动服务: {settings.HOST}:{settings.PORT}")
    uvicorn.run(
        "app.main:app",
        host=settings.HOST,
        port=settings.PORT,
        reload=True
    )